package com.xiaoq.matrix.endpoint;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.xiaoq.matrix.entity.ContentEntity;
import com.xiaoq.matrix.service.IContentService;

@Controller
@RequestMapping("/api/v1/socials")
public class SocialSharingController {
	@Autowired
	private IContentService contentSrv;
	
	private static final Logger LOG = LoggerFactory.getLogger(SocialSharingController.class);
	
	
	@GetMapping("/content")
	private void contentSharing(@RequestParam(value="contentId",required=true)String contentId,HttpServletRequest request, HttpServletResponse response){
		ContentEntity content = contentSrv.findContent(contentId);
		int quantity = content.getQuantity();
		quantity = quantity+1;
		content.setQuantity(quantity);
		contentSrv.addContent(content);
		String staticPath = content.getStaticPath();
		 try {
			response.sendRedirect(staticPath);
		} catch (IOException e) {
			LOG.error("重定向到文章的IO异常",e);
		}  
//		String contentdetailInfo = content.getDetailInfo();
//		response.setCharacterEncoding("utf-8");
//		try {
//			PrintWriter out = response.getWriter();
//			out.println(contentdetailInfo);
//		} catch (IOException e) {
//			e.printStackTrace();
//		}
	}
	

}
